using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace Avon
{
    /// <summary>
    /// termekekData
    /// </summary>
    public struct TermekekData
    {
        int m_TermekID;
        string m_Nev;
        int m_Alapar;
        int m_KategoriaID;
        string m_KategoriaNev;
        /// <summary>
        /// a termekek konstruktora id val es nevvel
        /// </summary>
        /// <param name="id">a termek id-ja</param>
        /// <param name="nev">a termek neve</param>
        public TermekekData(int id, string nev)
        {
            m_TermekID = id;
            m_Nev = nev;
            m_Alapar = 0;
            m_KategoriaID = 0;
            m_KategoriaNev = "";
        }
        /// <summary>
        /// getterek es setterek
        /// </summary>
        public int TermekID
        {
            get { return m_TermekID; }
            set { m_TermekID = value; }
        }
        public string Nev
        {
            get { return m_Nev; }
            set { m_Nev = value; }
        }
        public int Alapar
        {
            get { return m_Alapar; }
            set { m_Alapar = value; }
        }
        public int KategoriaID
        {
            get { return m_KategoriaID; }
            set { m_KategoriaID = value; }
        }
        public string KategoriaNev
        {
            get { return m_KategoriaNev; }
            set { m_KategoriaNev = value; }
        }

    }
    /// <summary>
    /// szarmaztatom a TermekDAlt a DAL osztalybol
    /// </summary>
    public class TermekDAL : DAL
    {
        /// <summary>
        /// le kerem a termekek lisjat es adataikat
        /// </summary>
        /// <param name="KategoriaID">a termek kategoriajanak id ja</param>
        /// <returns>SQL parancs alapjan vissza adja a termekek lsitajata</returns>
        public List<TermekekData> GetTermekList(int KategoriaID)
        {
            string query = "SELECT Termekek.TermekID, Termekek.Nev,Alapar,Kategoriak.Nev" +
                            " FROM Termekek, Kategoriak" +
                            " WHERE Termekek.KategoriaID = Kategoriak.KategoriaID";
            if (KategoriaID >= 0)
            {
                query += " AND Kategoriak.KategoriaID = " + KategoriaID;
            }

            string error = string.Empty;

            SqlDataReader rdr = ExecuteReader(query, ref error);
            Console.Write(error);
            List<TermekekData> TermekList = new List<TermekekData>();
            if (error == "OK")
            {
                while (rdr.Read())
                {
                    TermekekData item = new TermekekData();
                    item.TermekID = Convert.ToInt32(rdr[0]);
                    item.Nev = rdr[1].ToString();
                    item.Alapar = Convert.ToInt32(rdr[2]);
                    item.KategoriaNev = rdr[3].ToString();

                    TermekList.Add(item);
                }
            }
            CloseDataReader(rdr);
            return TermekList;
        }
        /// <summary>
        /// a modosit metodus
        /// </summary>
        /// <param name="TermekID"> a termek idja</param>
        /// <param name="Nev">a termek neve</param>
        /// <param name="Alapar">a termek ara</param>
        /// <param name="KategoriaID">a termek kategoriajanak idja</param>
        /// <returns>SQL parancs eredmenyet adja vissza</returns>
        public string ModositTermekek(int TermekID, string Nev, int Alapar, int KategoriaID)
        {
            string query = "UPDATE Termekek" +
                            " SET Nev = '" + Nev +
                            "', Alapar = " + Alapar +
                            ", KategoriaID = " + KategoriaID +
                            " WHERE TermekID = " + TermekID;
            return ExecuteNonQuery(query);

        }
        /// <summary>
        /// a beszur metodus
        /// </summary>
        /// <param name="Nev">a termek  neve</param>
        /// <param name="Alapar">a termek ara</param>
        /// <param name="KategoriaID">a termek kategoriajanak id ja</param>
        /// <returns>a beszur SQL parancs eredmenyet adja vissza</returns>
        public string BeszurTermek(string Nev, int Alapar, int KategoriaID)
        {
            string query = "INSERT INTO Termekek(Nev,Alapar,KategoriaID)" +
                            " VALUES ('" + Nev + "','" + Alapar + "','" + KategoriaID + "')";
            return ExecuteNonQuery(query);
        }
        /// <summary>
        /// a torol metodus
        /// </summary>
        /// <param name="TermekID"> a termek id ja, amit torolni szeretnenk</param>
        /// <returns>a torol SQL parancs eredmenye</returns>
        public string TorolTermek(int TermekID)
        {
            string query = "DELETE FROM Termekek WHERE TermekID = " + TermekID;
            return ExecuteNonQuery(query);
        }
    }
}
